package com.superzlc.vcontroller;

import com.superzlc.vcontroller.message.client.BroadcastClient;
import com.superzlc.vcontroller.message.client.Constants;
import com.superzlc.vcontroller.message.client.MessageClient;
import com.superzlc.vcontroller.message.client.MessageOp.VJoyPackage;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class CtrlBoard1Activity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

		// 隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		super.onCreate(savedInstanceState);
		ctrlBoard1View = new CtrlBoard1View(this);
		this.setContentView(ctrlBoard1View);

		threadMessage.start();
	}

	CtrlBoard1View ctrlBoard1View;

	public void test() {

	}

	MessageClient msgclient;
	VJoyPackage pkg = new VJoyPackage();
	Thread threadMessage = new Thread() {
		public void run() {
			while(true) {
				String ip = new BroadcastClient().requestIp();
				if (ip == null) {
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						break;
					}
				} else {
					Log.i("vjoy", "获取到服务端IP地址：" + ip);
					msgclient = new MessageClient(ip, Constants.PORT_FOR_MESSAGE);
				}
			}
			msgclient.startup();
			while(true) {
				// Log.i("vjoy", String.format("获取到轴：%d  %d  %d  %d",
				// ctrlBoard1View.getAsixSteeringWheel(),
				// ctrlBoard1View.getAsixClutch(),
				// ctrlBoard1View.getAsixBrakes(),
				// ctrlBoard1View.getAsixThrottle()));
				pkg.setAsix(0, ctrlBoard1View.getAsixSteeringWheel());
				pkg.setAsix(1, ctrlBoard1View.getAsixClutch());
				pkg.setAsix(6, ctrlBoard1View.getAsixBrakes());
				pkg.setAsix(7, ctrlBoard1View.getAsixThrottle());
				try {
					msgclient.sendData(pkg);
					Thread.sleep(5);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	};


}
